iT邦幫忙

2024 iThome 鐵人賽

DAY 8
0
自我挑戰組

從零開始學Python系列 第 8

[Day8] Python資料視覺化-Matplotlib

  • 分享至 

  • xImage
  •  

Matplotlib是Python中一種用於的可視化工具,是開源的(可以免費使用),也有一些部分是用 C、Objective-C 和 JavaScript 編寫的(具有平台兼容性)。

安裝

pip install matplotlib

import

import matplotlib
import matplotlib.pyplot as plt

Check Matplotlib Version

print(matplotlib.__version__)
#輸出:3.8.4

資料視覺化

  1. 基本圖形
plt.plot([1, 2, 3, 4])
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/201688119a6OJZeYWc.png

  1. 結合 Numpy 使用
xpoint = np.array([0, 50])
ypoint = np.array([0, 30])

plt.plot(xpoint, ypoint)
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811TVfKORzPQu.png

  1. 圖形加入 Marker 及線條顏色設定
ypoint = np.array([3, 9, 2, 5])

plt.plot(ypoint, marker = "o")
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811QWWf7s6lbQ.png

  • Makrer的形狀可參考:List of markers
  • 更改 Marker 大小及顏色:
ypoint = np.array([3, 9, 2, 5])

plt.plot(ypoint, marker = "o", ms = 10, mec = "r")
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811opzfHObcOE.png

ypoint = np.array([3, 9, 2, 5])

plt.plot(ypoint, marker = "o", ms = 10, mec = "r", mfc = "r")
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811bIotXzEy0q.png

  • 使用語法(syntax)設定
    maker|line|color
ypoint = np.array([3, 9, 2, 5])

plt.plot(ypoint, "o--g")
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811agTfPbMFKQ.png

  • Line style設定
ypoint = np.array([3, 9, 2, 5])

plt.plot(ypoint, linestyle = "dotted")
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811c02ztwOF7K.png

ypoint = np.array([3, 9, 2, 5])

plt.plot(ypoint, linestyle = "dotted", linewidth = 5)
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811oblp4eb9wt.png

Line style設定參考:Line style

  1. 繪製多條圖形
y1 = np.array([3, 4, 6, 7])
y2 = np.array([1, 6, 3, 2])

plt.plot(y1)
plt.plot(y2)

plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/201688112RFtFJBIH0.png

  1. 繪製多張圖形
#plot1
x = np.array([1, 2, 9, 4])
y = np.array([4, 6, 8, 9])

plt.subplot(1, 2, 1)
plt.plot(x, y)

#plot2
x = np.array([3, 5, 6, 8])
y = np.array([1, 3, 5, 7])

plt.subplot(1, 2, 2)
plt.plot(x, y)

https://ithelp.ithome.com.tw/upload/images/20240829/201688117jaa3SD0s8.png

  • 增加題目title
#plot1
x = np.array([1, 2, 9, 4])
y = np.array([4, 6, 8, 9])

plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.title("title1")

#plot2
x = np.array([3, 5, 6, 8])
y = np.array([1, 3, 5, 7])

plt.subplot(1, 2, 2)
plt.plot(x, y)
plt.title("title2")

https://ithelp.ithome.com.tw/upload/images/20240829/20168811VHW1pnaghx.png

  1. 繪製散點圖(scatter plot)
x = np.array([4, 2, 8, 1, 1, 5, 4, 7, 9, 8])
y = np.array([5, 1, 1, 2, 5, 3, 9, 9, 4, 7])

plt.scatter(x, y)
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811ss5U1WfLum.png

  • 設定散點大小、顏色、透明度
x = np.random.randint(100, size = 100) #生成一個 0 到 99 之間的100個隨機整數的一維數組
y = np.random.randint(100, size = 100)
colors = np.random.randint(100, size = 100)
size = 10 * np.random.randint(100, size = 100)

plt.scatter(x, y, c = colors, s = size, alpha = 0.8, cmap = "Set2")
plt.colorbar()
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811fFObswCJ2Q.png

  1. 增添colorbar
x = np.array([1, 8, 8, 2, 4])
y = np.array([3, 2, 6, 7, 8])
colors = np.array([70, 80, 50, 70, 40])

plt.scatter(x, y, c = colors, cmap = "flag")

plt.colorbar()
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811aNptCDyRla.png

  1. 繪製bar plot
  • 一般bar plot
x = np.array(["A", "B", "C", "D", "E"])
y = np.array([70, 80, 50, 10, 60])

plt.bar(x, y)
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811D7vVlYl4QG.png

  • horizontal bar plot
x = np.array(["A", "B", "C", "D", "E"])
y = np.array([70, 80, 50, 10, 60])

plt.barh(x, y)
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811CC980Undo2.png

  • 挑整bar的寬度
x = np.array(["A", "B", "C", "D", "E"])
y = np.array([70, 80, 50, 10, 60])

plt.bar(x, y, width = 0.3)
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/201688114TPRGdZEw9.png

  1. 繪製Histograms
x = np.random.normal(180, 10, 250) #平均180, 標準差10, 250個數據, 生成高斯分佈隨機數據

plt.hist(x)
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811wbzLbOVNhs.png

  1. 繪製圓餅圖(Pie chart)
x = np.array([6, 7, 5, 2, 4])
mlabels = ["A", "B", "C", "D", "E"]

plt.pie(x, labels = mlabels, startangle = 90) #圓餅圖會以 90 度角開始繪製
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811e3JShbcD91.png

  • 分離其中一塊(explode)
x = np.array([6, 7, 5, 2, 4])
mlabels = ["A", "B", "C", "D", "E"]
mexplode = [0.3, 0, 0, 0, 0]

plt.pie(x, labels = mlabels, explode = mexplode)
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811Ad6g7q0hIr.png

  • 加入陰影
x = np.array([6, 7, 5, 2, 4])
mlabels = ["A", "B", "C", "D", "E"]
mexplode = [0.3, 0, 0, 0, 0]

plt.pie(x, labels = mlabels, explode = mexplode, shadow = True)
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811noNOPqxnr0.png

  • 修改顏色以及加入legend
x = np.array([6, 7, 5, 2, 4])
mlabels = ["A", "B", "C", "D", "E"]
mexplode = [0.3, 0, 0, 0, 0]
mcolor = ["red", "blue", "green", "gray", "pink"]

plt.pie(x, labels = mlabels, explode = mexplode, shadow = True, colors = mcolor)
plt.legend()
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811E0S8tEJKcV.png

  • 顯示數值
x = np.array([6, 7, 5, 2, 4])
mlabels = ["A", "B", "C", "D", "E"]
mexplode = [0.3, 0, 0, 0, 0]
mcolor = ["red", "blue", "green", "gray", "pink"]

plt.pie(x, labels = mlabels, explode = mexplode, shadow = True, colors = mcolor, autopct = "%1.1f%%") #指定了顯示百分比時保留一位小數。
plt.legend()
plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811S5Ttp89wmI.png

  1. 匯出圖片
x = np.array([6, 7, 5, 2, 4])
mlabels = ["A", "B", "C", "D", "E"]
mexplode = [0.3, 0, 0, 0, 0]
mcolor = ["red", "blue", "green", "gray", "pink"]

plt.pie(x, labels = mlabels, explode = mexplode, shadow = True, colors = mcolor, autopct = "%1.2f%%") 
plt.legend()

plt.savefig("piechart.png")

plt.show()

https://ithelp.ithome.com.tw/upload/images/20240829/20168811sgAqxNGLtl.png


上一篇
[Day7] Python數據分析-Pandas基本用法
下一篇
[Day9] Python資料視覺化-Seaborn
系列文
從零開始學Python30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言